using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Text;
using System.Globalization;

/* =====================================================================

  File:      _Menu.ascx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Master page user control which displays a tree of product categories and subcategories.  Provides the
			 the ability to quickly navigate the product catalog.
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer 
{

    public partial class MenuControl : System.Web.UI.UserControl
    {


        //*******************************************************
        //
        // The Page_Load event on this page is used to obtain
        // from a database a list of all product categories
        // and databind it to an asp:datalist control.
        //
        // To optimize performance, this user control is output 
        // cached (varying based on the categoryId and selection
        // passed through the querystring.    
        //
        //*******************************************************

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (TreeView1.Nodes.Count > 0) return;

			String expansionParam = Request.Params["expansion"];
			String selectionParam = Request.Params["selection"];
			int expansionIndex = (expansionParam == null) ? -1 : Int32.Parse(expansionParam, CultureInfo.InvariantCulture);
			int selectionIndex = (selectionParam == null) ? -1 : Int32.Parse(selectionParam, CultureInfo.InvariantCulture);

			TreeView1.SelectedNodeStyle.ForeColor = Color.White;


			// Obtain list of menu categories and databind to tree control
			ProductsDB products = new ProductsDB();

			DataSet productsDs = products.GetProductTree();
			if (productsDs.Tables.Count > 1)
			{
				DataTable categoriesTable = productsDs.Tables["ProductCategory"];
				DataTable subcategoriesTable = productsDs.Tables["ProductSubcategory"];
				int categoryNumber = 0;
				int subcategoryNumber = 0;

				foreach (DataRow cdr in categoriesTable.Rows)
				{
					TreeNode categoryNode = new TreeNode(cdr["Name"].ToString(), cdr["ProductCategoryID"].ToString());
					TreeView1.Nodes.Add(categoryNode);
					foreach (DataRow scdr in cdr.GetChildRows("CategorySubCategories"))
					{
						TreeNode subcategoryNode = new TreeNode(scdr["Name"].ToString(), scdr["ProductSubcategoryID"].ToString());
						subcategoryNode.NavigateUrl = String.Format("productslist.aspx?SubcategoryID={0}&expansion={1}&selection={2}",
																	subcategoryNode.Value, categoryNumber, subcategoryNumber);
						categoryNode.ChildNodes.Add(subcategoryNode);
						if (subcategoryNumber == selectionIndex) subcategoryNode.Select();
						subcategoryNumber += 1;
					}
					if (categoryNumber == expansionIndex) categoryNode.Expand();
					categoryNumber += 1;
				}
			}
		}

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

		#region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {    

		}

		#endregion
    }
}
